<?php

/**
 * MLEPP - ManiaLive Extending Plugin Pack
 *
 * -- MLEPP Plugin --
 * @name Random Message
 * @date 04-01-2011
 * @version r1170
 * @website mlepp.trackmania.nl
 * @package MLEPP
 *
 * @author The MLEPP team
 * @copyright 2010 - 2011
 *
 * ---------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 * You are allowed to change things or use this in other projects, as
 * long as you leave the information at the top (name, date, version,
 * website, package, author, copyright) and publish the code under
 * the GNU General Public License version 3.
 * ---------------------------------------------------------------------
 */

namespace ManiaLivePlugins\MLEPP\RandomMessage;

use ManiaLive\Utilities\Console;
use ManiaLib\Utils\TMStrings as String;
use ManiaLive\DedicatedApi\Connection;
use \ManiaLive\PluginHandler\PluginHandler;
use ManiaLivePlugins\MLEPP\Core\Core;
use ManiaLivePlugins\MLEPP\Core\Mlepp;

class RandomMessage extends \ManiaLive\PluginHandler\Plugin {

	private $tick;
	private $config;

	 /**
	 * onInit()
	 * Function called on initialisation of ManiaLive.
	 *
	 * @return void
	 */
	function onInit() {
		$this->setVersion(1170);
		$this->setPublicMethod('getVersion');
		$this->tick = 0;
	}

	 /**
	 * onLoad()
	 * Function called on loading of ManiaLive.
	 *
	 * @return void
	 */
	function onLoad() {
		$this->enableDedicatedEvents();
		$this->enableTickerEvent();
		$this->config = Config::getInstance();
                $this->mlepp = Mlepp::getInstance();
		Console::println('[' . date('H:i:s') . '] [MLEPP] Plugin: RandomMessage r' . $this->getVersion());
		if ($this->config->type == "delay" || $this->config->type == "endChallenge") {
			
		} else {
			Console::println('[' . date('H:i:s') . '] [Random Message] Warning: Check your config.ini, the type should be either "delay" or "endChallenge" !');
		}
	}

	 /**
	 * onReady()
	 * Function called when ManiaLive is ready loading.
	 *
	 * @return void
	 */
	function onReady() {
	}

	 /**
	 * onTick()
	 * Function called every second, provides the delay messages.
	 *
	 * @return void
	 */
	function onTick() {
		if ($this->config->type == 'delay') {
			if ($this->tick != ($this->config->delay - 1)) {
				$this->tick++;
			} else {
				$this->tick = 0;
				$msgnum = rand(0, (count($this->config->messages) - 1));
				$message = $this->config->infoname . $this->config->infocolor . $this->config->messages[$msgnum];
				$this->mlepp->sendChat($message,false,$this);
			}
		}
	}

	 /**
	 * onEndChallenge()
	 * Function called on end of challenge, providing the end of challenge messages.
	 *
	 * @param mixed $rankings
	 * @param mixed $challenge
	 * @param mixed $wasWarmUp
	 * @param mixed $matchContinuesOnNextChallenge
	 * @param mixed $restartChallenge
	 * @return void
	 */
	function onEndMap($rankings, $map, $wasWarmUp, $matchContinuesOnNextMap, $restartMap) {
		if ($this->config->type == 'endChallenge') {
			$msgnum = rand(0, (count($this->config->messages) - 1));
			$message = $this->config->infoname . $this->config->infocolor . $this->config->messages[$msgnum];
			$this->mlepp->sendChat($message,false,$this);
		}
	}

}

?>